<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar fixed
                 left-arrow
                 @click-left="$router.back()"
                 title="文章详情"></van-nav-bar>
    <!-- 文章详情 -->
    <div class="detail"
         v-if="!loading">
      <div class="detail">
        <h3 class="title">{{article.title}}</h3>
        <div class="author">
          <van-image round
                     width="1rem"
                     height="1rem"
                     fit="fill"
                     :src="article.aut_photo" />
          <div class="text">
            <p class="name">{{article.aut_name}}</p>
            <p class="time">{{relativeTime(article.pubdate)}}</p>
          </div>
          <van-button round
                      size="small"
                      @click="toggleFollow">{{ article.is_followed ? "已关注" : "+关注" }}</van-button>
        </div>
        <div class="content">
          <div v-html="article.content"></div>
        </div>
        <van-divider>END</van-divider>
        <div class="zan">
          <van-button round
                      size="small"
                      hairline
                      type="primary"
                      plain
                      :icon="article.attitude===1?'good-job':'good-job-o'"
                      @click="fabulous">{{article.attitude===1?'已点赞':'点赞'}}</van-button>

        </div>
        <Commet></Commet>
      </div>
    </div>
    <van-loading size="24px"
                 vertical
                 v-if="loading"
                 class="article-loading">
      文章加载中...
    </van-loading>
  </div>
</template>

<script>
import { getArticle, followUser, unFollowUser, delLike, addLike } from '@/api/article.js'
import Commet from './Commet'
export default {
  name: 'ArticleIndex',
  components: {
    Commet
  },
  data () {
    return {
      article: {}, // 文章详情
      loading: true // 是否处于正在加载状态
    }
  },
  methods: {
    hGetArticle () {
      const id = this.$route.query.artID
      getArticle(id).then(res => {
        this.article = res.data.data
        this.loading = false
      }).catch(() => {
        console.log('当前文章没了')
        // 做处理
      })
    },

    // 切换+关注与已关注
    toggleFollow () {
      if (this.article.is_followed) {
        unFollowUser(this.article.aut_id.toString()).then(res => {
          this.$toast.success('取消关注')
        })
      } else {
        followUser(this.article.aut_id.toString()).then(res => {
          this.$toast.success('关注成功')
        })
      }
      this.article.is_followed = !this.article.is_followed
    },
    // 切换点赞与否
    fabulous () {
      // 当this.article.attitude为1的时候是“点赞”的状态，所以当我点击的时候要调用的是"取消点赞'
      // 接口，将并attitude的状态切换
      if (this.article.attitude === 1) {
        delLike(this.article.art_id.toString()).then(res => {
          this.$toast.success('取消点赞成功')
        })
      } else {
        addLike(this.article.art_id.toString()).then(res => {
          this.$toast.success('点赞成功')
        })
      }
      // 点击之后，attitude的状态切换,1和-1切换,通过这个来改变视图点赞窗口的变化
      this.article.attitude = this.article.attitude * -1
    }
  },
  mounted () {
    this.hGetArticle()
  }
}
</script>

<style lang='less' scoped>
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
